1
|
|
|
const Converter = require('../Converter/Converter'); |
|
|
|
|
2
|
|
|
const NotGeezArgumentException = require('../Exception/NotGeezArgumentException'); |
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* GeezParser parse the geez number to a queue. |
6
|
|
|
*/ |
7
|
|
|
module.exports = class GeezParser { |
8
|
|
|
/** |
9
|
|
|
* GeezParser constructor. |
10
|
|
|
* |
11
|
|
|
* @param $geez_number |
12
|
|
|
* |
13
|
|
|
* @throws NotGeezArgumentException |
14
|
|
|
*/ |
15
|
|
|
constructor($geez_number) { |
16
|
|
|
this.setGeezNumber($geez_number); |
17
|
|
|
this.parsed = null; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $geez_number |
22
|
|
|
* |
23
|
|
|
* @throws NotGeezArgumentException |
24
|
|
|
*/ |
25
|
|
|
setGeezNumber($geez_number) { |
26
|
|
|
if (typeof $geez_number !== typeof 'something') { |
27
|
|
|
throw new NotGeezArgumentException(typeof $geez_number); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
this.geez_number = $geez_number; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
getParsed() { |
34
|
|
|
return this.parsed; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Swing the magic wand and say the spell. |
39
|
|
|
*/ |
40
|
|
|
parse() { |
41
|
|
|
this.parsed = []; |
42
|
|
|
|
43
|
|
|
let $block = 0; |
44
|
|
|
|
45
|
|
|
let $length = this.getLength(this.geez_number); |
46
|
|
|
|
47
|
|
|
for (let $index = 0; $index < $length; $index++) { |
48
|
|
|
$block = this.parseCharacter($index, $block); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
this.pushToQueue($block, 1); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the length of the string. |
56
|
|
|
* |
57
|
|
|
* @param $geez_number |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
getLength($geez_number) { |
62
|
|
|
return $geez_number.length; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Parse a geez character. |
67
|
|
|
* |
68
|
|
|
* @param $index integer |
69
|
|
|
* @param $block integer |
70
|
|
|
* |
71
|
|
|
* @throws \Geezify\Exception\NotGeezArgumentException |
72
|
|
|
*/ |
73
|
|
|
parseCharacter($index, $block) { |
74
|
|
|
let $ascii_number = this.parseGeezAtIndex($index); |
75
|
|
|
|
76
|
|
|
if (this.isNotGeezSeparator($ascii_number)) { |
77
|
|
|
$block += $ascii_number; |
78
|
|
|
} else { |
79
|
|
|
this.pushToQueue($block, $ascii_number); |
80
|
|
|
$block = 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $block; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the ascii number from geez number string. |
88
|
|
|
* |
89
|
|
|
* @param $index |
90
|
|
|
* |
91
|
|
|
* @throws \Geezify\Exception\NotGeezArgumentException |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
parseGeezAtIndex($index) { |
96
|
|
|
let $geez_char = this.getCharacterAt(this.geez_number, $index); |
97
|
|
|
|
98
|
|
|
return parseInt(this.getAsciiNumber($geez_char)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Fetch z character at $index from the geez number string. |
103
|
|
|
* |
104
|
|
|
* @param $geez_number |
105
|
|
|
* @param $index |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
getCharacterAt($geez_number, $index) { |
110
|
|
|
return $geez_number.charAt($index); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Convert geez number character to ascii. |
115
|
|
|
* |
116
|
|
|
* @param $geez_number |
117
|
|
|
* |
118
|
|
|
* @throws NotGeezArgumentException |
119
|
|
|
* |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
getAsciiNumber($geez_number) { |
123
|
|
|
let $ascii_number = Object.values(Converter.GEEZ_NUMBERS).indexOf($geez_number); |
|
|
|
|
124
|
|
|
|
125
|
|
|
if ($ascii_number === -1) { |
126
|
|
|
throw new NotGeezArgumentException($geez_number); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return Object.keys(Converter.GEEZ_NUMBERS)[$ascii_number]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $ascii_number |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
isNotGeezSeparator($ascii_number) { |
138
|
|
|
return $ascii_number < 99; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Push to the queue. |
143
|
|
|
* |
144
|
|
|
* @param $block |
145
|
|
|
* @param $separator |
146
|
|
|
*/ |
147
|
|
|
pushToQueue($block, $separator) { |
148
|
|
|
this.parsed.push( |
149
|
|
|
{block: $block, separator: $separator} |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
}; |
153
|
|
|
|